package com.xunua.NaoZiKongBai.RegisterActivity;

import com.xunua.NaoZiKongBai.Base.BaseModel;
import com.xunua.NaoZiKongBai.Bean.Bmob.MyUser;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * @user XUNUA
 * @email xunua@outlook.com
 * @creat time 2020/5/15 19:13
 */
public class RegisterModel extends BaseModel<RegisterPresenter,RegisterContract.M> {
    public RegisterModel(RegisterPresenter mPresenter) {
        super(mPresenter);
    }

    @Override
    public RegisterContract.M getContract() {
        return new RegisterContract.M() {
            @Override
            public void request_register(String phone, String pwd,String yzm) {
                BmobSMS.verifySmsCode(phone, yzm, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            //开始验证手机号是否已经被注册
                            BmobQuery<MyUser> query = new BmobQuery<>();
                            query.addWhereEqualTo("mobilePhoneNumber",phone);
                            query.findObjects(new FindListener<MyUser>() {
                                @Override
                                public void done(List<MyUser> list, BmobException e) {
                                    if(e==null){
                                        if (list.size() > 0) {
                                            mPresenter.getContract().response_register(false,"此手机号已经注册过了");
                                        }else {
                                            MyUser user = new MyUser();
                                            user.setMobilePhoneNumber(phone);
                                            user.setMobilePhoneNumberVerified(true);
                                            user.setPassword(pwd);
                                            user.setUsername(phone);
                                            user.setPwd_look(pwd);
                                            user.setNick_name(phone);//用户昵称默认是手机号
//                                            user.setBan(false);//未被封禁         //数据库默认不被封禁  此条不需要
                                            user.signUp(new SaveListener<MyUser>() {
                                                @Override
                                                public void done(MyUser myUser, BmobException e) {
                                                    if (e == null) {
                                                        mPresenter.getContract().response_register(true,"注册成功");
                                                    }else {
                                                        switch (e.getErrorCode()){
                                                            case 202:
                                                                mPresenter.getContract().response_register(false,"账号已存在");
                                                                break;
                                                            case 301:
                                                                mPresenter.getContract().response_register(false,"手机号码必须是十一位数字");
                                                                break;
                                                            default:
                                                                mPresenter.getContract().response_register(false,"注册失败："+e.getMessage());
                                                                break;
                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    }else{
                                        mPresenter.getContract().response_register(false,"注册失败："+e.getMessage());
                                    }
                                }
                            });
                        } else {
                            mPresenter.getContract().response_register(false,"验证码错误，请重试");
                        }
                    }
                });
            }
        };
    }
}
